package com.yanqu.road.logic.activity.silkroad;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.silkroad.SilkRoadPlaceProgressInfo;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadEventConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadItemConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadPlaceConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadSeaConfig;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.pb.activity.SilkRoadProto;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SilkRoadConfig {
    private static Logger logger = LogManager.getLogger(SilkRoadConfig.class.getName());
    private static RandomHelper random = new RandomHelper();

    private Map<String, SilkRoadProto.SilkRoadConfigListMsg.Builder> languageConfigMap = new HashMap<>();
    private ActivityInfo activityInfo;
    private int EXCHANGE_SCORE_ITEM_ID = 101862;
    /**
     * 一键处理事件积分限制
     */
    private int oneKeyEventScoreLimit = 1000;
    /**
     * 探海勾使用获得积分
     */
    private int SILKROAD_ITEM_TICKET_SALVAGE_ITEM = 50;
    /**
     * 知名度道具
     */
    private int SILKROAD_ITEM_SCORE_ACHIEVEMENT = 101861;
    /**
     * 出海券
     */
    private int SILKROAD_ITEM_TICKET_NAVIGATION = 123350;
    /**
     * 探海勾
     */
    private int SILKROAD_ITEM_TICKET_SALVAGE = 123351;
    /**
     * 每步积分
     */
    private int SILKROAD_STEP_SCORE = 10;
    /**
     * 事件上限
     */
    private int SILKROAD_EVENT_NUM_LIMIT = 100;
    /**
     * 低级打捞消耗
     */
    private Property SILKROAD_SALVAGE_COST1 = new Property(123351, 1);
    /**
     * 高级打捞消耗
     */
    private Property SILKROAD_SALVAGE_COST2 = new Property(123351, 5);
    /**
     * 低级自动打捞消耗
     */
    private Property SILKROAD_SALVAGE_AUTOMATIC_COST1 = new Property(123351, 20);
    /**
     * 高级自动打捞消耗
     */
    private Property SILKROAD_SALVAGE_AUTOMATIC_COST2 = new Property(123351, 100);
    /**
     * 钩子参数
     */
    private List<Integer> hookParams = new ArrayList<>();
    /**
     * 打捞保底奖励
     */
    private List<List<Property>> refloatBaseRewards = new ArrayList<>();
    /**
     * 圈数奖励
     */
    private List<List<Property>> roundRewards = new ArrayList<>();
    /**
     * 步数随机列表
     */
    private List<Integer> stepRandomList = new ArrayList<>();

    /**
     * 地点配置
     */
    private List<SilkRoadPlaceConfig> placeList = new ArrayList<>();
    /**
     * 总路线点数量
     */
    private int totalRouteSize;
    /**
     * 事件触发概率     *
     */
    private long eventTriggerRate = 500;
    /**
     * 事件池
     */
    private List<NormalWeightItem> eventList = new ArrayList<>();
    /**
     * 额外事件列表
     */
    private Map<Integer, SilkRoadEventConfig> eventMap = new HashMap<>();
    /**
     * 打捞海域配置
     */
    private Map<Integer, Map<Integer, SilkRoadSeaConfig>> seaConfigMap = new HashMap<>();
    private Map<Integer, SilkRoadSeaConfig> seaItemConfigMap = new HashMap<>();
    private Map<Integer, List<Integer>> mapIdListMap = new HashMap<>();
    /**
     * 博物配置
     */
    private Map<Integer, SilkRoadItemConfig> silkRoadItemMap = new HashMap<>();

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public int getEXCHANGE_SCORE_ITEM_ID() {
        return EXCHANGE_SCORE_ITEM_ID;
    }

    public void setEXCHANGE_SCORE_ITEM_ID(int EXCHANGE_SCORE_ITEM_ID) {
        this.EXCHANGE_SCORE_ITEM_ID = EXCHANGE_SCORE_ITEM_ID;
    }

    public int getSILKROAD_ITEM_SCORE_ACHIEVEMENT() {
        return SILKROAD_ITEM_SCORE_ACHIEVEMENT;
    }

    public void setSILKROAD_ITEM_SCORE_ACHIEVEMENT(int SILKROAD_ITEM_SCORE_ACHIEVEMENT) {
        this.SILKROAD_ITEM_SCORE_ACHIEVEMENT = SILKROAD_ITEM_SCORE_ACHIEVEMENT;
    }

    public int getSILKROAD_ITEM_TICKET_NAVIGATION() {
        return SILKROAD_ITEM_TICKET_NAVIGATION;
    }

    public void setSILKROAD_ITEM_TICKET_NAVIGATION(int SILKROAD_ITEM_TICKET_NAVIGATION) {
        this.SILKROAD_ITEM_TICKET_NAVIGATION = SILKROAD_ITEM_TICKET_NAVIGATION;
    }

    public int getSILKROAD_ITEM_TICKET_SALVAGE() {
        return SILKROAD_ITEM_TICKET_SALVAGE;
    }

    public void setSILKROAD_ITEM_TICKET_SALVAGE(int SILKROAD_ITEM_TICKET_SALVAGE) {
        this.SILKROAD_ITEM_TICKET_SALVAGE = SILKROAD_ITEM_TICKET_SALVAGE;
    }

    public int getSILKROAD_STEP_SCORE() {
        return SILKROAD_STEP_SCORE;
    }

    public void setSILKROAD_STEP_SCORE(int SILKROAD_STEP_SCORE) {
        this.SILKROAD_STEP_SCORE = SILKROAD_STEP_SCORE;
    }

    public Property getSILKROAD_SALVAGE_COST1() {
        return SILKROAD_SALVAGE_COST1;
    }

    public int getSILKROAD_EVENT_NUM_LIMIT() {
        return SILKROAD_EVENT_NUM_LIMIT;
    }

    public void setSILKROAD_EVENT_NUM_LIMIT(int SILKROAD_EVENT_NUM_LIMIT) {
        this.SILKROAD_EVENT_NUM_LIMIT = SILKROAD_EVENT_NUM_LIMIT;
    }

    public void setSILKROAD_SALVAGE_COST1(Property SILKROAD_SALVAGE_COST1) {
        this.SILKROAD_SALVAGE_COST1 = SILKROAD_SALVAGE_COST1;
    }

    public Property getSILKROAD_SALVAGE_COST2() {
        return SILKROAD_SALVAGE_COST2;
    }

    public void setSILKROAD_SALVAGE_COST2(Property SILKROAD_SALVAGE_COST2) {
        this.SILKROAD_SALVAGE_COST2 = SILKROAD_SALVAGE_COST2;
    }

    public Property getSILKROAD_SALVAGE_AUTOMATIC_COST1() {
        return SILKROAD_SALVAGE_AUTOMATIC_COST1;
    }

    public void setSILKROAD_SALVAGE_AUTOMATIC_COST1(Property SILKROAD_SALVAGE_AUTOMATIC_COST1) {
        this.SILKROAD_SALVAGE_AUTOMATIC_COST1 = SILKROAD_SALVAGE_AUTOMATIC_COST1;
    }

    public Property getSILKROAD_SALVAGE_AUTOMATIC_COST2() {
        return SILKROAD_SALVAGE_AUTOMATIC_COST2;
    }

    public void setSILKROAD_SALVAGE_AUTOMATIC_COST2(Property SILKROAD_SALVAGE_AUTOMATIC_COST2) {
        this.SILKROAD_SALVAGE_AUTOMATIC_COST2 = SILKROAD_SALVAGE_AUTOMATIC_COST2;
    }

    public int getSILKROAD_ITEM_TICKET_SALVAGE_ITEM() {
        return SILKROAD_ITEM_TICKET_SALVAGE_ITEM;
    }

    public void setSILKROAD_ITEM_TICKET_SALVAGE_ITEM(int SILKROAD_ITEM_TICKET_SALVAGE_ITEM) {
        this.SILKROAD_ITEM_TICKET_SALVAGE_ITEM = SILKROAD_ITEM_TICKET_SALVAGE_ITEM;
    }

    public List<Integer> getHookParams() {
        return hookParams;
    }

    public void setHookParams(List<Integer> hookParams) {
        this.hookParams = hookParams;
    }

    public List<List<Property>> getRefloatBaseRewards() {
        return refloatBaseRewards;
    }

    public void setRefloatBaseRewards(List<List<Property>> refloatBaseRewards) {
        this.refloatBaseRewards = refloatBaseRewards;
    }

    public List<List<Property>> getRoundRewards() {
        return roundRewards;
    }

    public void setRoundRewards(List<List<Property>> roundRewards) {
        this.roundRewards = roundRewards;
    }

    public List<SilkRoadPlaceConfig> getPlaceList() {
        return placeList;
    }

    public void setPlaceList(List<SilkRoadPlaceConfig> placeList) {
        this.placeList = placeList;

        int result = 0;
        for (SilkRoadPlaceConfig place : placeList) {
            result += place.getRouteSize() + 1; //大地点也算一个
        }

        this.totalRouteSize = result;
    }

    public int getTotalRouteSize() {
        return totalRouteSize;
    }

    public void setTotalRouteSize(int totalRouteSize) {
        this.totalRouteSize = totalRouteSize;
    }

    public Map<Integer, SilkRoadEventConfig> getEventMap() {
        return eventMap;
    }

    public void setEventMap(Map<Integer, SilkRoadEventConfig> eventMap) {
        this.eventMap = eventMap;
    }

    public Map<Integer, Map<Integer, SilkRoadSeaConfig>> getSeaConfigMap() {
        return seaConfigMap;
    }

    public void setSeaConfigMap(Map<Integer, SilkRoadSeaConfig> seaConfigMap) {
        this.seaItemConfigMap = seaConfigMap;
        for (SilkRoadSeaConfig silkRoadSeaConfig : seaConfigMap.values()) {
            int seaKey = silkRoadSeaConfig.getType() * 1000 + silkRoadSeaConfig.getMapId();
            Map<Integer, SilkRoadSeaConfig> map = this.seaConfigMap.get(seaKey);
            if (map == null) {
                map = new HashMap<>();
                this.seaConfigMap.put(seaKey, map);
            }
            map.put(silkRoadSeaConfig.getId(), silkRoadSeaConfig);

            List<Integer> list = this.mapIdListMap.get(silkRoadSeaConfig.getType());
            if (list == null) {
                list = new ArrayList<>();
                this.mapIdListMap.put(silkRoadSeaConfig.getType(), list);
            }
            list.add(silkRoadSeaConfig.getMapId());
        }
    }

    public Map<Integer, SilkRoadItemConfig> getSilkRoadItemMap() {
        return silkRoadItemMap;
    }

    public void setSilkRoadItemMap(Map<Integer, SilkRoadItemConfig> silkRoadItemMap) {
        this.silkRoadItemMap = silkRoadItemMap;
    }

    /**
     * 初始化活动配置
     *
     * @return
     */
    public void parseConfig(Map<String, ActivityConfig> activityConfigMap) {

        this.setEXCHANGE_SCORE_ITEM_ID(activityConfigMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue());
        this.setSILKROAD_ITEM_SCORE_ACHIEVEMENT(activityConfigMap.get("SILKROAD_ITEM_SCORE_ACHIEVEMENT").getIntValue());
        this.setSILKROAD_ITEM_TICKET_NAVIGATION(activityConfigMap.get("SILKROAD_ITEM_TICKET_NAVIGATION").getIntValue());
        this.setSILKROAD_ITEM_TICKET_SALVAGE(activityConfigMap.get("SILKROAD_ITEM_TICKET_SALVAGE").getIntValue());
        this.setSILKROAD_STEP_SCORE(activityConfigMap.get("SILKROAD_STEP_SCORE").getIntValue());
        this.setSILKROAD_SALVAGE_COST1(PropertyHelper.parseStringToProperty(activityConfigMap.get("SILKROAD_SALVAGE_COST1").getValue()));
        this.setSILKROAD_SALVAGE_COST2(PropertyHelper.parseStringToProperty(activityConfigMap.get("SILKROAD_SALVAGE_COST2").getValue()));
        this.setSILKROAD_SALVAGE_AUTOMATIC_COST1(PropertyHelper.parseStringToProperty(activityConfigMap.get("SILKROAD_SALVAGE_AUTOMATIC_COST1").getValue()));
        this.setSILKROAD_SALVAGE_AUTOMATIC_COST2(PropertyHelper.parseStringToProperty(activityConfigMap.get("SILKROAD_SALVAGE_AUTOMATIC_COST2").getValue()));
        List<Integer> hookParameter = StringUtils.stringToIntegerList(activityConfigMap.get("SILKROAD_HOOK_PARAMETER").getValue(), "\\|");
        this.setHookParams(hookParameter);

        List<List<Property>> listList = PropertyHelper.parseNewStringToPropertyList(activityConfigMap.get("SILKROAD_ITEM_SALVAGE_REWARDA").getValue());
        this.setRefloatBaseRewards(listList);

        this.stepRandomList = StringUtils.stringToIntegerList(activityConfigMap.get("SILKROAD_COMPASS_WEIGHTS").getValue(), "\\|");
        this.setSILKROAD_EVENT_NUM_LIMIT(activityConfigMap.get("SILKROAD_EVENT_NUM_LIMIT").getIntValue());
        this.setSILKROAD_ITEM_TICKET_SALVAGE_ITEM(activityConfigMap.get("SILKROAD_ITEM_TICKET_SALVAGE_ITEM").getIntValue());
        String[] split = activityConfigMap.get("SILKROAD_EVENT_LIST").getValue().split("=");
        this.eventTriggerRate = Long.parseLong(split[0]);
        this.eventList = NormalWeightItem.parseWeightItemList(split[1]);

        this.setRoundRewards(PropertyHelper.parseNewStringToPropertyList(activityConfigMap.get("SILKROAD_TURNS_REWARDA").getValue()));
        this.setOneKeyEventScoreLimit(activityConfigMap.get("SILKROAD_EVENT_LIST_COND").getIntValue());

    }

    /**
     * 初始配置消息体
     */
    public void initConfigMsg(SilkRoadProto.SilkRoadConfigListMsg.Builder builder, String language) {
        this.languageConfigMap.put(language, builder);
    }

    public SilkRoadProto.SilkRoadConfigListMsg.Builder getConfigMsg(String language) {
        SilkRoadProto.SilkRoadConfigListMsg.Builder builder = this.languageConfigMap.get(language);
        return builder;
    }

    /**
     * 获取玩家位置信息
     *
     * @param currentIndex
     * @return
     */
    public SilkRoadPlaceProgressInfo getPlaceInfo(int currentIndex) {
        SilkRoadPlaceProgressInfo placeInfo = new SilkRoadPlaceProgressInfo();
        try {
            //计算轮数
            placeInfo.setRound((currentIndex - 1) / totalRouteSize);
            //当前轮数的下标
            while (currentIndex > totalRouteSize) {
                currentIndex = currentIndex - totalRouteSize;
            }
            //计算大地点 小地点
            int index = 0;
            for (int i = 0; i < placeList.size(); i++) {
                SilkRoadPlaceConfig placeTemp = placeList.get(i);
                //大地点+当前包含小地点
                index = index + placeTemp.getRouteSize() + 1;
                if (currentIndex <= index) {
                    placeInfo.setPlaceId(placeTemp.getId());
                    placeInfo.setSilkRoadPlace(placeTemp);
                    placeInfo.setRoutProgress(placeTemp.getRouteSize() - (index - currentIndex));
                    break;
                }
            }
        } catch (Exception e) {
            logger.error("getPlaceInfo", e);
        }
        return placeInfo;
    }

    /**
     * 获取随机的步数
     *
     * @return
     */
    public int getRandomStepNum() {
        List<Integer> weightList = this.stepRandomList;
        int sumWeight = 0;
        for (int weight : weightList) {
            sumWeight += weight;
        }
        int randomValue = random.next(0, sumWeight);

        int tempWeight = 0;
        for (int i = 0; i < weightList.size(); i++) {
            tempWeight += weightList.get(i);
            if (tempWeight > randomValue) {
                return i + 1;
            }
        }
        return 0;
    }

    /**
     * 判断海域是否存在
     */
    public boolean isExistSea(int seaType) {
        return this.mapIdListMap.containsKey(seaType);
    }

    /**
     * 随机一个地图
     */
    public int randomSeaMapId(int seaType) {
        List<Integer> list = this.mapIdListMap.get(seaType);
        if (list == null || list.isEmpty()) {
            return 0;
        }
        int index = random.next(0, list.size());
        return list.get(index);
    }
    /**
     * 获取海域所有打捞物
     */
    public List<SilkRoadSeaConfig> getAllSeaRefloat(int seaType, int mapId) {
        int seaKey = seaType * 1000 + mapId;
        List<SilkRoadSeaConfig> seaList = new ArrayList<>();
        Map<Integer, SilkRoadSeaConfig> seaMap = this.seaConfigMap.get(seaKey);
        for (SilkRoadSeaConfig sea : seaMap.values()) {
            seaList.add(sea);
        }
        return seaList;
    }

    /**
     * 获取海域打捞物配置
     */
    public SilkRoadSeaConfig getSeaItemConfig(int itemId) {
        return this.seaItemConfigMap.get(itemId);
    }

    /**
     * 博物配置
     */
    public SilkRoadItemConfig getSilkRoadItemConfig(int itemId) {
        return this.silkRoadItemMap.get(itemId);
    }

    public long getEventTriggerRate() {
        return eventTriggerRate;
    }

    public List<NormalWeightItem> getEventList() {
        return eventList;
    }

    public int getOneKeyEventScoreLimit() {
        return oneKeyEventScoreLimit;
    }

    public void setOneKeyEventScoreLimit(int oneKeyEventScoreLimit) {
        this.oneKeyEventScoreLimit = oneKeyEventScoreLimit;
    }
}
